package com.runewbie.pafcmall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: li_jian
 * @version: 1.0
 * @date: 2020/5/31 23:06
 * @description： Elasticsearch 配置文件
 * <p>
 * SpringBoot 集成 ES 的步骤：
 *  1、导入依赖
 *      https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-maven.html
 *  2、编写 ES 配置，给容器中注入一个 RestHighLevelClient，用来操作 9200 端口
 *      https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html
 *  3、参照官方API
 */
@Configuration
public class PafcmallElasticsearchConfig {

    /**
     * RequestOptions类保存应在同一应用程序中的许多请求之间共享的部分请求。 您可以创建一个单例实例，并在所有请求之间共享它
     */
    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esRestHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                // 这里可以配置多个 es服务，我当前服务不是集群，所以目前只配置一个
                RestClient.builder(
                        new HttpHost("192.168.56.10", 9200, "http")));

        return client;
    }
}
